<?php
class CommentsController extends AppController {
	var $name = 'Comments';	
	function beforeFilter(){
		parent::beforeFilter();
		if (substr($this->params['action'], 0, 6) == 'admin_')
			$this->layout = "admin";
		else $this->layout = "lam";
	}		
	/** 
	 * Cho phép người dùng comment tin quảng cáo, rao vặt.
	 *
	 * @Author:	Do Thi Phuong Thao
	 * @Date: 	20/04/11
	 * @Email: 	dophuongthao109@gmail.com
	 */
	function add() {
		if (!empty($this->data)) {		
			$this->Comment->create();
			if ($this->Comment->save($this->data)) {
				$this->redirect(array('controller'=>'posts','action' => 'view', $this->data['Comment']['post_id']));
			} else {
				$this->Session->setFlash(__('Có lỗi khi thêm bình luận. Xin thử lại', true));
			}
		}		
	}	
	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for comment', true));
			$this->redirect(array('action'=>'index'));
		}
		$cm = $this->Comment->findById($id);
		$post_id = $cm['Comment']['post_id'];
		if ($this->Comment->delete($id)) {
			$this->Session->setFlash(__('Comment deleted', true));
			$this->redirect(array('controller'=>'posts','action' => 'view', $post_id));
		}
		$this->Session->setFlash(__('Comment was not deleted', true));
		$this->redirect(array('controller'=>'posts','action' => 'view', $post_id));
	}	
	function admin_index() {
		$this->Comment->recursive = 0;
		$this->set('comments', $this->paginate());
	}
	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for comment', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Comment->delete($id)) {
			$this->Session->setFlash(__('Comment deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Comment was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>